package com.viphuli.app.tool.adapter;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.common.MyApp;
import com.viphuli.app.tool.utils.ImageUtils;

public class HolidayLeaveTypeAdapter extends BaseAdapter {
	private List<ArrangeType> list;
	private List<Integer> selectList = new ArrayList<Integer>(2);

	public HolidayLeaveTypeAdapter(List<ArrangeType> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int index) {
		return list.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null || convertView.getTag() == null) {
			convertView = ViewUtils.inflateView(MyApp.getInstance(), R.layout.frag_arrange_action_type_list_item);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		ArrangeType arrangeTypeInfo = list.get(position);

		holder.bg.setBackgroundColor(Color.parseColor(arrangeTypeInfo.getColor()));
		holder.name.setText(arrangeTypeInfo.getName());
		
		if(StringUtils.isNotBlank(arrangeTypeInfo.getDuration())){
			
			holder.duration.setText(arrangeTypeInfo.getDuration());
			
		}else{
			
			int day = arrangeTypeInfo.getHour() / 8;
			int hour = arrangeTypeInfo.getHour() % 8;

			StringBuilder sb = new StringBuilder();
			if (day > 0) {
				sb.append(day + "天");
			}
			if (hour > 0) {
				sb.append(hour + "小时");
			}

			holder.duration.setText("时长" + sb.toString());
			
		}

		ImageUtils.load(holder.img, arrangeTypeInfo.getIcon());

		if (!selectList.isEmpty() && selectList.contains(position)) {
			holder.check.setBackgroundResource(R.drawable.ic_arrange_type_selected);
		} else {
			holder.check.setBackgroundResource(R.drawable.ic_arrange_type_un_selected);
		}

		return convertView;
	}

	public void check(int selected) {
		selectList.add(selected);
		notifyDataSetChanged();
	}

	public List<Integer> getCheck() {
		return selectList;
	}

	protected class ViewHolder {

		@Bind(R.id.id_home_item_icon_bg)
		View bg;

		@Bind(R.id.id_home_item_icon_img)
		ImageView img;

		@Bind(R.id.id_home_item_icon_name)
		TextView name;

		@Bind(R.id.id_home_item_arrange_type_duration)
		TextView duration;

		@Bind(R.id.id_home_item_arrange_type_check)
		View check;

		public ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}
}